﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Budgeteer.Services;
using Budgeteer.Services.Common;
using Budgeteer.Services.DataProviders;

namespace Budgeteer.WebSite
{
    public partial class _Default:System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            RegisterChartInitializationScripts();
        }

        private void RegisterChartInitializationScripts()
        {
            StringBuilder scripts = new StringBuilder();
            scripts.AppendLine("function VisifireInit(){");
            scripts.AppendLine("var vChart1 = new Visifire('ClientBin/Visifire.xap',500,300);");
            scripts.AppendLine("vChart1.setLogLevel(1); // disable logging");
            scripts.Append("vChart1.setDataXml(\"");
            scripts.Append(GetChartXmlData().Replace("\r","").Replace("\n",""));
            scripts.AppendLine("\");");
            scripts.AppendLine("vChart1.render(\"VisifireChart0\");");
            scripts.AppendLine("}");

            ClientScript.RegisterClientScriptBlock(typeof(string),"PostBackScripts",scripts.ToString(),true);
        }

        static string GetChartXmlData()
        {
            ConfigService config = new ConfigService();
            CategoryDataProvider categoryDataProvider = new CategoryDataProvider(Config.ConnectionString);
            CategoryService categoryService = new CategoryService(categoryDataProvider,config);
            string chartXml = categoryService.GetChartXmlData(categoryService.GetCategoriesAmount());
            return chartXml.Replace("\"","\'");
        }
    }
}
